package group.newsReader.Service.Impl;

import group.newsReader.Service.NlpService;
import group.newsReader.Utils.ApiUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;

@Service
public class NlpServiceImpl implements NlpService {
    @Autowired
    private ApiUtil apiUtil;

    @Override
    public String getDeepSeek(String query) throws IOException, InterruptedException {
        String jsonInputString = "{\n" +
                "  \"model\": \"deepseek-chat\",\n" +
                "  \"messages\": [\n" +
                "    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n" +
                "    {\"role\": \"user\", \"content\": \"" + query + "\"}\n" +
                "  ],\n" +
                "  \"stream\": false\n" +
                "}";
        String urlString = "https://api.deepseek.com/chat/completions";
        System.out.println(urlString);

        // 创建 HttpClient
        HttpClient client = HttpClient.newBuilder()
                .build();

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(urlString))
                .header("Content-Type", "application/json")
                .header("Authorization", "Bearer " + apiUtil.getDeepSeekApiKey())  // 使用你自己的 API 密钥
                .POST(HttpRequest.BodyPublishers.ofString(jsonInputString, StandardCharsets.UTF_8))
                .build();

        // 发送请求并获取响应
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        // 打印响应状态码和返回数据
        System.out.println("Response Status Code: " + response.statusCode());
        System.out.println("Response Body: " + response.body());

        // 解析 JSON 响应并提取 content
        JSONObject responseJson = new JSONObject(response.body());
        JSONArray choices = responseJson.getJSONArray("choices");
        JSONObject choice = choices.getJSONObject(0);
        JSONObject message = choice.getJSONObject("message");
        String content = message.getString("content");

        return content;  // 返回 content 的内容
    }

    @Override
    public String translate(String content) throws IOException, InterruptedException {
        String query="请帮我将以下文本翻译成中文，注意只要文本对应翻译，不要出现其余文字，" +
                "比如我问”fox“你只需回答“狐狸。你要翻译的文本如下：" + content;
        // 将换行符 (\n) 和回车符 (\r) 替换为空格
        query = query.replaceAll("[\r\n]+", " ");
        System.out.println(query);
        return getDeepSeek(query);
    }

    @Override
    public String generateQuestions(String content) throws IOException, InterruptedException {
        String query="请根据以下内容帮我生成3-5道英文选择题，用来检验用户是否读懂这篇文章：" +
                "注意，你只需要给出包含题目和正确答案的json，选项用A,B,C,D表示：" + content;
        // 将换行符 (\n) 和回车符 (\r) 替换为空格
        query = query.replaceAll("[\r\n]+", " ");
        System.out.println(query);
        return getDeepSeek(query);
    }
}
